package channeltype

import (
	"fmt"
	"sync"
	"time"
)

var wg sync.WaitGroup

func consumer(queue <-chan int) {
	//可以把一个双向channel传给单向channel参数
	defer wg.Done()
	for {
		data, ok := <-queue
		if !ok {
			break
		}

		fmt.Println(data, ok)
		time.Sleep(time.Second)
		//queue <- 2
	}
}

func ChannelTypeLearn() {
	//channel type
	//有没有缓冲：1.有缓冲 2.无缓冲
	//双向还是单向：
	//var msg chan<- int
	//var msg <-chan int
	//msg = make(chan<- int, 10)
	//msg <- 1
	//msg <- 2
	//<-msg

	//msg := make(chan int)
	//wg.Add(1)
	//go consumer(msg)
	//msg <- 1
	//fmt.Println(<-msg)
	//close(msg)
	//wg.Wait()

	msg := make(chan int)
	wg.Add(1)
	go consumer(msg)
	msg <- 1
	close(msg)
	wg.Wait()
}
